<template>
  <div class="invest-money">
    <el-row style="margin:20px 0;">
      <el-col :span="12" style="height:10px"></el-col>
      <el-col :span="8" style="height:10px"></el-col>
      <el-col :span="4">
        <el-button type="primary" icon="el-icon-edit" @click="showEdit = true,editObj={}">添加充值选项</el-button>
      </el-col>
    </el-row>

    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="上次操作时间">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.createdAt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="充值金额（元）">
        <template slot-scope="scope">{{scope.row.payMoney}}</template>
      </el-table-column>
      <!-- <el-table-column label="充值比例">
        <template slot-scope="scope">￥1:{{scope.row.proportion}}欧币</template>
      </el-table-column> -->
      <el-table-column label="赠送方案包(个)">
        <template slot-scope="scope">{{scope.row.Presented}}</template>
      </el-table-column>
      <el-table-column label="大礼包">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.presentStatus == 1 ?true :false"
            active-color="#13ce66"
            inactive-color="#ccc"
            @change="isGivePresent(scope.$index,scope.row)"
          ></el-switch>
          <el-button
            size="mini"
            v-if="scope.row.presentStatus == 1"
            type="primary"
            @click="goEditPresent(scope.row)"
          >编辑</el-button>
          <el-button size="mini" v-else>编辑</el-button>
        </template>
      </el-table-column>
      <!-- <el-table-column label="总欧币数">
        <template slot-scope="scope">{{scope.row.balance}}</template>
      </el-table-column> -->
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <h4 style="padding-left:10px;">充值说明</h4>
    <!-- <el-input
      type="textarea"
      :rows="10"
      placeholder="请输入充值说明"
      v-model="explain"
      style="margin:20px 0 0 10px;width:500px;"
    ></el-input>-->
    <Tinymce v-model="explain" :height="300" style="width: 705px;"></Tinymce>
    <el-button type="primary" @click="submitExplain()">提交</el-button>
    <el-dialog title="金额编辑" :visible.sync="showEdit">
      <el-form
        :model="editObj"
        :rules="rules"
        ref="editObj"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="充值金额" prop="payMoney">
          <el-input-number :step="0.5" :precision="2" v-model="editObj.payMoney"></el-input-number>
        </el-form-item>
        <!-- <el-form-item label="充值比例" prop="proportion">
          ￥1元:
          <el-input-number
            style="width:157px;"
            :step="0.5"
            :precision="2"
            v-model="editObj.proportion"
          ></el-input-number>
        </el-form-item> -->
        <el-form-item label="赠送方案包" required prop="Presented">
          <el-input-number :step="0.5" :precision="2" v-model="editObj.Presented"></el-input-number>
        </el-form-item>
        <!-- 共{{(((editObj.proportion*10000)*(editObj.payMoney*10000))/10000+(editObj.Presented*10000))/10000}} -->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showEdit = false">取 消</el-button>
        <el-button type="primary" @click="save()">确 定</el-button>
      </span>
    </el-dialog>

    <!-- <el-dialog title="大礼包编辑" :visible.sync="editPresent">
      <el-form
        label-width="100px"
      >
        <el-form-item label="大礼包">
          <el-checkbox-group v-model="giveType">
            <el-checkbox label="线上课程" name="giveType"></el-checkbox>
            <el-checkbox label="商品" name="giveType"></el-checkbox>
            <el-checkbox label="线下活动" name="giveType"></el-checkbox>
          </el-checkbox-group>



        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editPresent = false">取 消</el-button>
        <el-button type="primary" @click="savePresent()">确 定</el-button>
      </span>
    </el-dialog>-->
  </div>
</template>
<script>
import Tinymce from "@/components/Tinymce";

import {
  getInvestList,
  saveInvest,
  deleteInvest,
  explain
} from "@/api/finance";
export default {
  data() {
    return {
      tableData: [],
      // editPresent: true,
      showEdit: false,
      editObj: {
        Presented: "",
        payMoney: "",
        proportion: "",
        payId: "",
        balance: ""
      },
      explain: "",
      rules: {
        payMoney: [
          { required: true, message: "请输入充值金额", trigger: "blur" }
        ],
        proportion: [
          { required: true, message: "请输入充值比例", trigger: "blur" }
        ],
        Presented: [
          {
            type: "mun",
            required: true,
            message: "请输入赠送欧币",
            trigger: "change"
          }
        ]
      },
      giveType: ["线上课程"],
      courseList: [],
      goodsList: [],
      activity: []
    };
  },
  watch: {
    // giveType(val) {
    //   for (let i = 0; i < this.giveType.length; i++) {
    //     const element = this.giveType[i];
    //     if (element == "线上课程") {
    //       console.log(1);
    //     } else if (element == "商品") {
    //       console.log(2);
    //     } else if (element == "线下活动") {
    //       console.log(3);
    //     }
    //   }
    // }
  },
  methods: {
    goEditPresent(val) {
      localStorage.setItem("course", JSON.stringify(val.course)); 
      localStorage.setItem("activity", JSON.stringify(val.activity)); 
      localStorage.setItem("goods", JSON.stringify(val.goods)); 
      this.$router.push({
        path: "investMoney/editPresent",
        query: {
          payId: val.payId
        }
      });
    },
    isGivePresent(index, val) {
      if (val.presentStatus == 1) {
        val.presentStatus = 2;
      } else {
        val.presentStatus = 1;
      }
      let data = {
        presentStatus: val.presentStatus,
        payId: val.payId
      };
      saveInvest(data).then(res => {
        console.log(res);
      });
    },
    getList() {
      getInvestList().then(res => {
        this.tableData = res.data.data.payList;
        this.explain = res.data.data.explain.explain;
      });
    },
    async submitExplain() {
      let data = {
        explain: this.explain
      };
      await explain(data).then(res => {
        this.$message(res.data.data);
      });
    },
    handleEdit(index, val) {
      this.editObj = {
        Presented: val.Presented,
        payMoney: val.payMoney,
        proportion: val.proportion,
        payId: val.payId,
        payId: val.payId,
        payId: val.payId,
        payId: val.payId
      };
      this.showEdit = true;
    },
    async saveData() {
      this.showEdit = false;
      await saveInvest(this.editObj).then(res => {
        this.$message(res.data.data);
      });
      this.getList();
    },
    save() {
      this.editObj.balance =
        Number(this.editObj.payMoney * this.editObj.proportion) +
        Number(this.editObj.Presented);
      this.saveData();
    },
    async handleDelete(index, val) {
      console.log(val.payId);
      var query = {
        payId: val.payId
      };
      await deleteInvest(query).then(res => {
        console.log(res);
      });
      this.getList();
    }
  },
  components: {
    Tinymce
  },
  mounted() {
    this.getList();
    //     getInvestList().then(res =>{
    //         console.log(res);

    //     })
    // );
  }
};
</script>

<style scoped>
</style>
